<?php
namespace Qwadmin\Model;
class WithdrawLogModel extends \Think\Model{
    /**
     *  新增创建提现记录
     */
    public function add_log($data){
        return M("withdraw_log")->add($data);
    }
    
    /**
     *  更改提现状态
     *  @param string $orderid 订单ID号
     *  @param array $data 订单信息
     */
    public function setOrderInfo($orderid , $data){
        return M("withdraw_log")->where(['out_biz_no' => $orderid])->save($data);
    }
	/**
     *  查询提现信息
     *  @param string $id 提现申请ID号
     *  @param array $data 订单信息
     */
    public function selOrderInfo($id){
        return M("withdraw_log")->where(['id' => $id])->find();
    }
    
    /** 
     *  获取用户提现记录
     *  @param array $map 筛选条件
     */
    public function getList($pram){
        $page = intval($pram['page']) ?  $pram['page'] : 1;         //当前分页页码
        $pageSize = intval($pram['rows']) ? $pram['rows'] : 10;     //每页显示记录数
		$where = [
            'uid' => is_array($pram['uid'])?['in',$pram['uid']]:$pram['uid'],
        ];
		if(!empty($pram['status'])){
			$where['status'] = $pram['status']==2?0:$pram['status'];
		}
		if (!empty($pram['start_date'])) {
			$where["DATE_FORMAT(create_time , '%Y-%m-%d')"]= array(array('EGT', date('Y-m-d',$pram['start_date'])));
        }
        if (!empty($pram['end_date'])) {
			$where["DATE_FORMAT(create_time , '%Y-%m-%d')"][]= array('lt', date('Y-m-d',$pram['end_date']));
        }
        $first = $pageSize * ($page - 1);
		
        $data['list']  = M("withdraw_log")    
							->where($where)
                            ->order("create_time desc")
                            ->limit($first, $pageSize)
                            ->select();
		//echo M('withdraw_log')->getLastsql();
		foreach($data['list'] as $key=>$value){
			$data['list'][$key]['amount'] = sprintf('%.2f', $value['amount']);
		}	

        $data["count"] = M("withdraw_log")->where($where)->count('id');
        return $data;
    }
    /**
	* 获取提现申请统计
	**/
	function getWLog_count($pram){
		$prefix = C('DB_PREFIX');
        $where = [
            'uid' => is_array($pram['uid'])?['in',$pram['uid']]:$pram['uid']
        ];
		if (!empty($pram['start_date'])) {
			$where["DATE_FORMAT(create_time , '%Y-%m-%d')"]= array(array('EGT', date('Y-m-d',$pram['start_date'])));
        }
        if (!empty($pram['end_date'])) {
			$where["DATE_FORMAT(create_time , '%Y-%m-%d')"][]= array('lt', date('Y-m-d',$pram['end_date']));
        }
        
        //统计总额
        $total_money  = M('withdraw_log')->field('sum(amount) as amount')
							->where($where)
                            ->find();
		$where['status']=0;
		$apply_money = M('withdraw_log')->field('sum(amount) as amount')
							->where($where)
                            ->find();
						
		
		$data['total_money']= sprintf('%.2f', $total_money['amount']);//总充值
		$data['apply_money']=sprintf('%.2f', $apply_money['amount']);//申请中
		$data['play_money']=sprintf('%.2f', $total_money['amount']-$apply_money['amount']);//已打款
		
        return $data;
	}
    /**
     *  是否达到提现条件（一级代理商，一天只提一次）
     *  @param int $uid 代理商ID
     */
    public function canWithdraw($uid){
        $where = "DATE_FORMAT(pay_time , '%Y-%m-%d') = '" . date("Y-m-d") . "' and uid={$uid}" ;
        $count = M("withdraw_log")->where($where)->count("id");
        //用户是否为一级商户
        $userInfo = D("user")->findUser(['uid' => $uid]);
        if($userInfo['level'] > 1){
            return ['status' => 0 , 'msg' => '请找您的上级代理商提现' ];
        }
        
        if(!empty($count)){
            return ['status' => 0 , 'msg' => '亲，一天只能提现一次哦！' ];
        }
        
        return ['status' => 1 , 'msg' => ''];
    }
	
	
}
